﻿namespace OboutInc
{
    using System;
    using System.Collections;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Xml;

    public class ScriptResponse
    {
        private bool _keepLast;
        private static Type[] _numericTypes = new Type[] { typeof(byte), typeof(sbyte), typeof(decimal), typeof(double), typeof(float), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(short), typeof(ushort) };
        private StringBuilder _responseData;
        private StringBuilder _responseScript;
        private string _responseType;
        private bool _userExplicit;

        public ScriptResponse(bool keepLast)
        {
            this._keepLast = keepLast;
            this._userExplicit = false;
        }

        internal void ClearResponseData()
        {
            if (this._responseData != null)
            {
                this._responseData = new StringBuilder();
            }
        }

        public string Render(bool ommitIfEmpty)
        {
            if ((ommitIfEmpty && (this._responseScript == null)) && (this._responseData == null))
            {
                return "";
            }
            StringBuilder builder = new StringBuilder("<pbext" + (this._userExplicit ? " e=\"1\"" : "") + ">");
            if (this._responseScript != null)
            {
                builder.Append("<script><![CDATA[");
                builder.Append(this._responseScript.ToString());
                builder.Append("]]></script>");
            }
            else
            {
                builder.Append("<script />");
            }
            if (this._responseData != null)
            {
                builder.Append("<data type=\"" + ((this.ResponseType == null) ? "s" : this.ResponseType) + "\">");
                if (this._responseType == "s")
                {
                    builder.Append("<![CDATA[");
                    builder.Append(this._responseData.ToString());
                    builder.Append("]]>");
                }
                else
                {
                    builder.Append(this._responseData.ToString());
                }
                builder.Append("</data>");
            }
            else
            {
                builder.Append("<data isNull=\"true\" />");
            }
            builder.Append("</pbext>");
            return builder.ToString();
        }

        internal static bool typeIsNumeric(Type type)
        {
            foreach (Type type2 in _numericTypes)
            {
                if (type == type2)
                {
                    return true;
                }
            }
            return false;
        }

        internal static void WriteArray(XmlTextWriter xtw, ICollection array)
        {
            IEnumerator enumerator = array.GetEnumerator();
            enumerator.Reset();
            while (enumerator.MoveNext())
            {
                object current = enumerator.Current;
                object obj3 = null;
                if (array is IDictionary)
                {
                    obj3 = Regex.Replace(((IDictionaryEnumerator) enumerator).Entry.Key.ToString(), "[^a-zA-Z0-9_#]", "");
                    current = ((IDictionaryEnumerator) enumerator).Entry.Value;
                }
                if (current == null)
                {
                    xtw.WriteStartElement("s");
                    xtw.WriteAttributeString("isNull", "true");
                    if (obj3 != null)
                    {
                        xtw.WriteAttributeString("name", obj3.ToString());
                    }
                }
                else if (current is ICollection)
                {
                    xtw.WriteStartElement("c");
                    if (obj3 != null)
                    {
                        xtw.WriteAttributeString("name", obj3.ToString());
                    }
                    WriteArray(xtw, (ICollection) current);
                }
                else
                {
                    if (typeIsNumeric(current.GetType()))
                    {
                        xtw.WriteStartElement("n");
                    }
                    else if (current.GetType() == typeof(bool))
                    {
                        xtw.WriteStartElement("b");
                    }
                    else
                    {
                        xtw.WriteStartElement("s");
                    }
                    if (obj3 != null)
                    {
                        xtw.WriteAttributeString("name", obj3.ToString());
                    }
                    if (current != null)
                    {
                        xtw.WriteCData(current.ToString());
                    }
                }
                xtw.WriteEndElement();
            }
            xtw.Flush();
        }

        public void WriteResponse(object obj)
        {
            if (obj is ICollection)
            {
                this.ResponseType = "c";
                StringWriter w = new StringWriter();
                XmlTextWriter xtw = new XmlTextWriter(w);
                xtw.WriteStartElement("c");
                WriteArray(xtw, (ICollection) obj);
                xtw.WriteEndElement();
                this.WriteResponse(w.ToString());
            }
            else
            {
                if (typeIsNumeric(obj.GetType()))
                {
                    this.ResponseType = "n";
                }
                else if (obj is bool)
                {
                    this.ResponseType = "b";
                }
                else
                {
                    this.ResponseType = "s";
                }
                if (this._responseData != null)
                {
                    this._responseData.Append(obj.ToString());
                }
                else
                {
                    this._responseData = new StringBuilder(obj.ToString());
                }
            }
        }

        public void WriteScript(string str)
        {
            this.WriteScript(str, false);
        }

        internal void WriteScript(string str, bool start)
        {
            str = str.Trim();
            str = str + (str.EndsWith(";") ? "" : ";");
            if (this._responseScript != null)
            {
                if (start)
                {
                    this._responseScript.Insert(0, str);
                }
                else
                {
                    this._responseScript.Append(str);
                }
            }
            else
            {
                this._responseScript = new StringBuilder(str);
            }
        }

        public bool KeepLast
        {
            get
            {
                return this._keepLast;
            }
        }

        internal string ResponseData
        {
            get
            {
                if (this._responseData == null)
                {
                    return string.Empty;
                }
                return this._responseData.ToString();
            }
        }

        internal string ResponseScript
        {
            get
            {
                if (this._responseScript == null)
                {
                    return string.Empty;
                }
                return this._responseScript.ToString();
            }
        }

        private string ResponseType
        {
            get
            {
                return this._responseType;
            }
            set
            {
                if (this._responseType == null)
                {
                    this._responseType = value;
                }
            }
        }

        internal bool UserExplicit
        {
            get
            {
                return this._userExplicit;
            }
            set
            {
                this._userExplicit = value;
            }
        }
    }
}

